﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Grishko.UI;
using OutdoorLib.BaseEntity.Infrastructure;
using Grishko.DataTier;

public partial class Ctl_ctlTimeFilterPanel : UserControl,IFilterEditor,  ITimePeriodFilter
{
    

    private const string CloneFilterKey = "CloneFilterKey";

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        IitControls();
        if (!IsPostBack)
            CacheTools.ResetSessionCache(CloneFilterKey);

    }
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            DataBind();
            ImChange();

        }

    }

    public FilterPanel _FilterPanel
    {
        get
        {
            return FilterPanel1;
        }
    }


    protected virtual IFilterPanel FilterPanel
    {
        get { return FilterPanel1; }
    }

    protected virtual IFilterEditor FilterEditor
    {
        get { return this; }
    }

    Filter _FilterClone = null;
    protected Filter GetCloneFilterDataSource()
    {
        if (null != _FilterClone)
            return _FilterClone;

        if (CacheTools.IsSessionCacheObject(CloneFilterKey))
            _FilterClone = (Filter)CacheTools.GetSessionCacheObect(CloneFilterKey);
        else
        {
            Filter BaseFilter = FilterPanel.DataSource();
            if (null == BaseFilter)
                            return null;
            _FilterClone = (Filter)FilterPanel.DataSource().Clone();
            CacheTools.SetSessionCacheObject(CloneFilterKey, _FilterClone);
        }

        return _FilterClone;
    }

    public bool ShowEditor
    {
        get
        {

            object text = ViewState["ShowEditor"];
            if (null == text)
                return false;
            else
                return Convert.ToBoolean(text);
        }
        set
        {
            ViewState["ShowEditor"] = value;
        }
    }

    private void IitControls()
    {
        FilterEditor.DataSource = GetCloneFilterDataSource;
        ctlFilterEditor1.DataSource = GetCloneFilterDataSource;

    }


    protected void btnShowFilter_Click(object sender, ImageClickEventArgs e)
    {
        ShowEditor = (!ShowEditor);
        DataBind();
        updFilter.Update();
    }




    protected void btnFind_Click(object sender, ImageClickEventArgs e)
    {
        this.dtStart = Convert.ToDateTime(txtStart.Text);
        this.dtFinish  = Convert.ToDateTime(txtFinish.Text);
        ImChange();
        
    }
  


    #region Члены IFilterEditor

    public Func<Filter> DataSource
    {
        get;
        set;
    }

    public Func<bool> ImChange
    {
        get { return ctlFilterEditor1.ImChange; }
        set { ctlFilterEditor1.ImChange = value; }
    }

    #endregion

    //#region Члены ITimePeriodFilter
    
    //public DateTime dtStart
    //{
    //    get
    //    {
    //        DateTime dtStart = DateTime.Now;            
    //        DateTime.TryParse (txtStart.Text,out dtStart);            
    //        return dtStart;
    //    }
    //    set
    //    {
    //        txtStart.Text = value.ToShortDateString();
            
    //    }
    //}

    //public DateTime dtFinish
    //{
    //    get
    //    {
    //        DateTime dtFinish = DateTime.Now;
    //        DateTime.TryParse(txtFinish.Text, out dtFinish);
    //        return dtFinish;
    //    }
    //    set
    //    {
    //        txtFinish.Text = value.ToShortDateString();
    //    }
    //}

    //protected string sDtStart
    //{
    //    get
    //    {
    //        if (null == FilterAsTimePeriod)
    //            return string.Empty;

    //        return FilterAsTimePeriod.dtStart.ToShortDateString();
    //    }
    //}

    //protected string sDtFinish
    //{
    //    get
    //    {
    //        if (null == FilterAsTimePeriod)
    //            return string.Empty;

    //        return FilterAsTimePeriod.dtFinish.ToShortDateString ();
    //    }
    //}
    

    //#endregion

    #region Члены ITimePeriodFilter

    private ITimePeriodFilter TimePeriodFilterSource
    {
        get
        {
            if (null == DataSource)
                return null;
            return (DataSource() as ITimePeriodFilter);
        }
    }


    public DateTime dtStart
    {
        get
        {
            if(null==TimePeriodFilterSource)
                return GlobalConstants .MinDate;

            return TimePeriodFilterSource.dtStart; 
        }
        set
        {
            TimePeriodFilterSource.dtStart =value ;
        }
    }

    public DateTime dtFinish
    {
        get
        {
            if (null == TimePeriodFilterSource)
                return GlobalConstants.MaxDate;

            return TimePeriodFilterSource.dtFinish ; 
        }
        set
        {
            TimePeriodFilterSource.dtFinish=value;
        }
    }

    protected string sDtStart
    {
        get { return dtStart.ToShortDateString(); }
    }

    protected string sDtFinish
    {
        get { return dtFinish.ToShortDateString(); }
    }


    #endregion
}
